// 2025/11/10
// 逆波兰表达式求值

class Solution {
public:
    int cal(int num1, int num2, string op)
    {
        if(op == "+")
            return num1 + num2;
        else if(op == "-")
            return num1 - num2;
        else if(op == "*")
            return num1 * num2;
        else if(op == "/")
            return num1 / num2;
        else
            return 0;
    }

    int evalRPN(vector<string>& tokens) {
        stack<int> opNum;
        for(auto &token : tokens)
        {
            if(token == "+" || token == "-" || token == "*" || token == "/")
            {
                int num2 = opNum.top(); opNum.pop();
                int num1 = opNum.top(); opNum.pop();
                opNum.emplace(cal(num1, num2, token));
            }
            else
            {
                opNum.emplace(stoi(token));
            }
        }
        return opNum.top();
    }
};